<template>
  <div class="app-container">
    <div>
      <el-form :inline="true" :model="listQuery" class="demo-form-inline" @submit.native.prevent>
        <el-form-item label="名称">
          <el-input v-model="listQuery.q_couponName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="发放日期">
          <el-date-picker
            v-model="listQuery.q_sendTime"
            type="datetime"
            placeholder="选择日期时间" size="small">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用日期">
          <el-date-picker
            v-model="listQuery.q_useTime"
            type="datetime"
            placeholder="选择日期时间" size="small">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            class="toolbar-lable-right"
            v-model="listQuery.q_status"
            placeholder="请选择" size="small">
            <el-option
              v-for="item in actStatus"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="渠道">
          <el-select
            class="toolbar-lable-right"
            v-model="listQuery.q_channel"
            placeholder="请选择" size="small">
            <el-option
              v-for="item in channel"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商家">
          <el-select
            class="toolbar-lable-right"
            v-model="listQuery.q_sellerId"
            placeholder="请选择"
            size="small"
          >
            <el-option
              v-for="item in sellers"
              :label="item.name"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="this.getList">查询</el-button>
          <el-button icon="el-icon-refresh" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="button-table-margin">
        <el-button size="small" type="primary" @click="audit"  v-permission="['/admin/promotion/coupon/doaudit']">审核</el-button>
        <el-button size="small" type="primary" @click="details"  v-permission="['/admin/promotion/couponuser/list']">发放详情</el-button>
      </div>
      <el-table
        ref="singleTable"
        v-loading="listLoading"
        :data="dataList"
        border
        fit
        highlight-current-row
        @current-change="handleCurrentChange"
        style="width: 100%">
        <el-table-column
          type="index"
          align="center"
          width="50">
        </el-table-column>
        <el-table-column property="sellerName" width="200" label="店铺名称"></el-table-column>
        <el-table-column property="couponName" width="200" label="优惠券名称"></el-table-column>
        <el-table-column property="prefix" width="200" label="优惠券前缀"></el-table-column>
        <el-table-column property="couponValue" width="200" label="优惠券面值"></el-table-column>
        <el-table-column property="minAmount" width="200" label="适用最低订单金额"></el-table-column>
        <el-table-column property="sendStartTime" width="200" label="发放开始时间"></el-table-column>
        <el-table-column property="sendEndTime" width="200" label="发放结束时间"></el-table-column>
        <el-table-column property="useStartTime" width="200" label="使用起始时间"></el-table-column>
        <el-table-column property="useEndTime" width="200" label="使用截止时间"></el-table-column>
        <el-table-column property="personLimitNum" width="200" label="会员限制数量"></el-table-column>
        <el-table-column property="totalLimitNum" width="200" label="总数量"></el-table-column>
        <el-table-column property="receivedNum" width="200" label="已发放数量"></el-table-column>
        <el-table-column property="type" width="150" label="优惠券类型">
          <template slot-scope="scope">
            {{ codeBox.COUPON_TYPE[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column property="channel" width="150" label="应用渠道">
          <template slot-scope="scope">
            {{ codeBox.CHANNEL[scope.row.channel] }}
          </template>
        </el-table-column>
        <el-table-column property="status" width="150" label="状态">
          <template slot-scope="scope">
            {{ codeBox.ACT_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column property="updateUserName" width="200" label="最后修改用户"></el-table-column>
        <el-table-column property="updateTime" width="200" label="最后修改时间"></el-table-column>
        <el-table-column property="createUserName" width="200" label="创建用户"></el-table-column>
        <el-table-column property="createTime" width="200" label="创建时间"></el-table-column>
        <el-table-column property="remark" width="150" label="优惠券描述"></el-table-column>
        <el-table-column property="auditOpinion" width="200" label="审核意见"></el-table-column>
        <el-table-column fixed="right" label="操作" width="70" align="center">
          <template slot-scope="scope">
            <el-button @click="auditRow(scope.row)" type="text" size="small">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <div>
      <el-dialog title="发放详情" :visible.sync="dialogFormVisible">
        <el-form :inline="true" ref="couponuserListQuery" :model="couponuserListQuery" class="demo-form-inline">
          <el-form-item label="序列号">
            <el-input v-model="couponuserListQuery.q_couponSn" size="small"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="small" @click="getCouponuserList">查询</el-button>
            <el-button icon="el-icon-refresh" size="small" @click="resetCouponuser">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table
          ref="singleTable"
          :data="couponuserList"
          height="450"
          border
          fit
          highlight-current-row
          style="width: 100%">
        <!--  <el-table-column
            type="index"
            align="center"
            width="50">
          </el-table-column> -->
          <el-table-column property="memberName" width="200" label="会员名称"></el-table-column>
          <el-table-column property="couponSn" width="200" label="序列号"></el-table-column>
          <el-table-column property="canUse" width="200" label="可使用次数"></el-table-column>
          <el-table-column property="receiveTime" width="200" label="领取时间"></el-table-column>
          <el-table-column property="orderSn" width="200" label="订单Sn"></el-table-column>
          <el-table-column property="useTime" width="200" label="使用时间"></el-table-column>
        </el-table>
        <Pagination v-show="couponuserTotal>0" :total="couponuserTotal" :page.sync="couponuserListQuery.page" :limit.sync="couponuserListQuery.limit" @pagination="getCouponuserList" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listCoupon, listCouponUser } from '@/api/promotionbasics'
import Pagination from '@/components/Pagination'
import { Message } from 'element-ui'
import { getCodeManager,codeToArray, getSellers} from '@/api/all'
export default {
  name: 'exchangelist',
  components: { Pagination },
  data() {
    return {
      currentRow: null,
      dataList: [],
      actStatus: [],
      channel: [],
      sellers: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        q_couponName: '',
        q_sendTime: '',
        q_useTime: '',
        q_status: '',
        q_channel: '',
        q_sellerId: ''
      },
      dialogFormVisible: false,
      couponuserList: [],
      couponuserTotal: 0,
      couponuserListQuery: {
        page: 1,
        limit: 20,
        q_couponSn: ''
      }
    }
  },
  created() {
    getCodeManager('ACT_STATUS,CHANNEL,COUPON_TYPE', () => {
      this.actStatus = codeToArray('ACT_STATUS');
      this.channel = codeToArray('CHANNEL');
    })
    this.getList()
    getSellers().then(response => {
      this.sellers=[
        ...response.data.rows.map((item)=>{return  {
          name:item.sellerName,
          value:item.id
        }})
      ]
    })
  },
  methods:{
    handleCurrentChange(val) {
      // 选中当前行数据
      this.currentRow = val
    },
    // 获取列表数据
    getList() {
      this.listLoading = true
      listCoupon(this.listQuery).then(response => {
        this.dataList = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    reset() {
      this.listQuery = {
        page: 1,
        limit: 20,
        q_couponName: '',
        q_sendTime: '',
        q_useTime: '',
        q_status: '',
        q_channel: '',
        q_sellerId: ''
      }
      this.getList()
    },
    resetCouponuser() {
      this.couponuserListQuery = {
        page: 1,
        limit: 20,
        q_couponSn: ''
      }
      this.getCouponuserList()
    },
    audit(){
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      if(this.currentRow.status != 2) {
        Message.error('只有提交审核状态才能进行审核操作')
        return
      }
      this.$router.push({
        path: "/promotionbasics/couponaudit",
        query: { id: this.currentRow.id }
      })
    },
    auditRow(currentRowDo){
      if(currentRowDo.status != 2) {
        Message.error('只有提交审核状态才能进行审核操作')
        return
      }
      this.$router.push({
        path: "/promotionbasics/couponaudit",
        query: { id: currentRowDo.id }
      });
    },
    details() {
      if(this.currentRow == null) {
        Message.error('请选择一条记录')
        return
      }
      this.dialogFormVisible = true
      this.getCouponuserList()
    },
    getCouponuserList() {
      let newListQyery={
        q_couponId: this.currentRow.id,
        ...this.couponuserListQuery
      }
      listCouponUser(newListQyery).then(response => {
        this.couponuserList = response.data.rows
        this.couponuserTotal = response.data.total
      })
    }
  }
}
</script>

<style>
</style>
